package com.luhuiguo.gwt.mvp.dispatcher.server;

import java.util.Map;

import com.google.inject.Singleton;
import com.luhuiguo.gwt.mvp.dispatcher.shared.rpc.Action;
import com.luhuiguo.gwt.mvp.dispatcher.shared.rpc.Result;

/**
 * 
 * @author LuHuiguo
 * @version $Id: DefaultActionHandlerRegistry.java 11 2009-12-26 13:39:17Z luhuiguo $
 */
@Singleton
public class DefaultActionHandlerRegistry implements InstanceActionHandlerRegistry {

    private final Map<Class<? extends Action<?>>, ActionHandler<?, ?>> handlers;

    public DefaultActionHandlerRegistry() {
        handlers = new java.util.HashMap<Class<? extends Action<?>>, ActionHandler<?, ?>>( 100 );
    }

    public void addHandler( ActionHandler<?, ?> handler ) {
        handlers.put( handler.getActionType(), handler );
    }

    public boolean removeHandler( ActionHandler<?, ?> handler ) {
        return handlers.remove( handler.getActionType() ) != null;
    }

    @SuppressWarnings("unchecked")
    public <A extends Action<R>, R extends Result> ActionHandler<A, R> findHandler( A action ) {
        return (ActionHandler<A, R> ) handlers.get( action.getClass() );
    }

    public void clearHandlers() {
        handlers.clear();
    }

}
